***********************
*Time Series Analysis for the Social Sciences
*Box-Steffensmeier, Freeman, Hitt, and Pevehouse
*
*Chapter 7 replication code
************************

************************
*Figure 7.1
*Use fracint_sims.dta
*Source: Simulated
************************
tsset time

ac frac_int
ac near_int

*************************
*Figure 7.2
*Use IEM 2008 Obama.dta
*Source: http://tippie.uiowa.edu/iem/markets/data_nomination08.html
*************************

tsline avgprice

tsline pricechange


*************************
*Figure 7.3
*Use approval gallup.dta
*Source: http://hdl.handle.net/1902.1/17923 IQSS Dataverse Network
*************************

tsline papp

*************************
*Figure 7.4
*Use pres7804.dta
*Source: http://hdl.handle.net/1902.1/15582 
*************************

line presap date, yaxis(1) yscale(range(50 90)) || ///
line npros1bw date, yaxis(2) yscale(range(50 150)) 

*Note: to run a similar model to Lebo and Box-Steffensmeier, use the code below
*The original replication materials for this paper are done in RATS software
*These materials are available at: http://hdl.handle.net/1902.1/15582

set more off
mgarch dcc (presap = , noconstant) (npros1bw = ,noconstant), arch(1) garch(1) technique(bhhh)

***************************
*Figure 7.5
*Use forecastCurve_sims.dta
*Source: simulated, see Chapter 7 simulated data forecasting.r
***************************

gen quad = time * time

reg ser4 time quad if time < 4001
predict curve4
tsline ser4 || tsline curve4

****************************
*Figures 7.6-7.10
*Use forecast_sims.dta
*Source: simulated, see Chapter 7 simulated data forecasting.r
****************************
gen time = as_ts_time2
drop as_ts_time2
tsset time
tsappend, add(5)

*Figure 7.6

graph twoway (line rw1 time if time > 375) ///
(rarea rw1l rw1u time if time > 375) ///
(line rw1_01 time if time > 375), xlabel(375(10)405) xscale(range(375(10)405))

*Figure 7.7

arima ar2_1, arima(2,0,0)

predict ar2f, y
predict ar2mse, mse

gen ar2l = ar2f - (1.96 * sqrt(ar2mse))
gen ar2u = ar2f + (1.96 * sqrt(ar2mse))

graph twoway (line ar2_1 time if time > 375) ///
(rarea ar2l ar2u time if time > 375) ///
(line ar2f time if time > 375), xlabel(375(10)405) xscale(range(375(10)405))

*Figure 7.8

arima ma1, arima(0,0,1)

predict ma1f, y 
predict ma1mse, mse

gen ma1l = ma1f - (1.96 * sqrt(ma1mse))
gen ma1u = ma1f + (1.96 * sqrt(ma1mse))


graph twoway (line ma1 time if time > 375) ///
(rarea ma1l ma1u time if time > 375) ///
(line ma1f time if time > 375), xlabel(375(10)405) xscale(range(375(10)405))

*Figure 7.9

arima ar11, arima(1,1,0)

predict ar11f, y dynamic(401)
predict ar11mse, mse dynamic(401)

gen ar11l = ar11f - (1.96 * sqrt(ar11mse))
gen ar11u = ar11f + (1.96 * sqrt(ar11mse))

graph twoway (line ar11 time if time > 375) ///
(rarea ar11l ar11u time if time > 375) ///
(line ar11f time if time > 375), xlabel(375(10)405) xscale(range(375(10)405))

*Figure 7.10

arima ar011, arima(0,1,1)

predict ar011f, y dynamic(401)
predict ar011mse, mse dynamic(401)

gen ar011l = ar011f - (1.96 * sqrt(ar011mse))
gen ar011u = ar011f + (1.96 * sqrt(ar011mse))


graph twoway (line ar011 time if time > 375) ///
(rarea ar011l ar011u time if time > 375) ///
(line ar011f time if time > 375), xlabel(375(10)405) xscale(range(375(10)405))

************************
*Figures 7.11-7.14
*Use UCR.dta
*Source: http://www.fbi.gov/about-us/cjis/ucr/ucr
*************************

*Figure 7.11

tsline homicideth

*Figure 7.12

arima homicideth, arima(2,0,0) sarima(0,0,1,12)

predict homf, y
predict hommse, mse

gen homl = homf - (1.96 * sqrt(hommse))
gen homu = homf + (1.96 * sqrt(hommse))


graph twoway (rarea homl homu mdate) ///
(scatter homicideth mdate) ///
(line homf mdate)

gen fcasterror = homicideth - homf

graph twoway area fcasterror mdate


*Figure 7.13

tsappend, add(12)

arima homicideth, arima(2,0,0) sarima(0,0,1,12)

predict homfdyn, y dynamic(tm(1993m12))
predict hommsedyn, mse dynamic(tm(1993m12))

gen homdynl = homfdyn - (1.96 * sqrt(hommsedyn))
gen homdynu = homfdyn + (1.96 * sqrt(hommsedyn))

graph twoway (rarea homdynl homdynu mdate if t > 228) ///
(line homfdyn mdate if t > 228) (connected homicideth mdate if t > 216) 

*Figure 7.14

arima homicideth, arima(2,0,0) sarima(0,0,1,12)

predict homfdyn2, y dynamic(tm(1992m1))
predict hommsedyn2, mse dynamic(tm(1992m1))
 

gen homdyn2l = homfdyn2 - (1.96 * sqrt(hommsedyn2))
gen homdyn2u = homfdyn2 + (1.96 * sqrt(hommsedyn2))

graph twoway (rarea homdyn2l homdyn2u mdate if t > 205 & t < 229) ///
(line homf mdate if t > 205 & t < 229) ///
(connected homicideth mdate if t > 181 & t < 229) ///
(line homfdyn2 mdate if t > 205 & t < 229)
